<template>
	<view class="addpeo">
		<view class="add_info font12 font-ef9140">本院实行实名制就诊,请如实填写就诊人信息,如因信息错误产生的一切后果自行负责.</view>
		<!-- 切换 -->
		<view class="add_head">
			<view v-for="(item,index) in headlist" :key="index" class="head_bos flex-column-center"
				@click="Onhead(item)">
				<text :class="headindex == item.id?'head_titlea':'head_titleb'">{{item.title}}</text>
				<view v-if="headindex == item.id"></view>
			</view>
		</view>
		<!-- 内容 -->
		<view class="add_bos flex-row-start">
			<view class="add_title font-323232 font15 font-b">姓名</view>
			<view class="add_box">
				<input class="font15 font-323232" placeholder="请输入您的真实姓名" :value="objdata.name" maxlength="8" @input="Onname"
					placeholder-class="placeholderClass" />
			</view>
		</view>
		<view class="add_bos flex-row-start">
			<view class="add_title font-323232 font15 font-b">手机号码</view>
			<view class="add_box">
				<input class="font15 font-323232" placeholder="请输入您的手机号" :value="objdata.mobile"  type="number" maxlength="11" @input="Onmobile"
					placeholder-class="placeholderClass" />
			</view>
		</view>
		<view class="add_bos flex-row-start">
			<view class="add_title font-323232 font15 font-b">身份证</view>
			<view class="add_box">
				<input class="font15 font-323232" placeholder="请输入您的身份证号码" :value="objdata.id_card"  type="idcard" maxlength="18"
					@input="Onidcard" placeholder-class="placeholderClass" />
			</view>
		</view>
		<!-- 注册按钮 -->
		<button class="add_but" @click='Onregister' :loading="loading">立 即 绑 定
		</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: '',
				id: null,
				Pid: null,
				headindex: 1,
				headlist: [{
						title: '本人',
						id: 1
					},
					{
						title: '他人',
						id: 2
					}
				],
				loading: false, //加载
				objdata: {
					is_my: 1,
					name: '',
					id_card: '',
					mobile: '',
				},
			}
		},
		onLoad: function(options) {
			if (options.Pid) {
				this.Pid = options.Pid
				this.Oninfo()
			}
			if (options.id) {
				this.id = options.id
			}
		},
		methods: {
			Oninfo() {
				this.$Http({
					url: 'PhysicalUserInfo',
					data: {
						physical_user_id:this.Pid
					}
				}).then(res => {
					if (res.code == 1) {
						this.headindex = res.data.is_my
						// this.objdata.is_my = res.data.is_my
						this.objdata.name = res.data.name
						this.objdata.id_card = res.data.id_card
						this.objdata.mobile = res.data.mobile
					}
				})
			},
			//切换
			Onhead(res) {
				this.headindex = res.id
				this.objdata.is_my = 1
			},
			//姓名
			Onname(e) {
				this.objdata.name = e.detail.value
			},
			//手机号码
			Onmobile(e) {
				this.objdata.mobile = e.detail.value
			},
			//身份证
			Onidcard(e) {
				this.objdata.id_card = e.detail.value
			},
			//提交
			Onregister() {
				var that = this;
				if (that.objdata.mobile == '') {
					wx.showToast({
						title: '请输入手机号码',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if (!(/^1[34578]\d{9}$/.test(that.objdata.mobile)) || that.objdata.mobile.length != 11) {
					wx.showToast({
						title: '手机号码格式错误',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if (!this.$base.checkCard(this.objdata.id_card)) {
					wx.showToast({
						title: '身份证错误',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if (this.objdata.name == '') {
					wx.showToast({
						title: '体检人姓名不能为空',
						icon: 'none',
						duration: 2000
					})
					return
				}
				let url = ''
				let data = this.objdata
				data.is_my = this.headindex
				if (this.Pid) {
					url = 'editPhysicalUser'
					data.physical_user_id = this.Pid
				} else {
					url = 'addPhysicalUser'
				}
				this.$Http({
					url: url,
					data: data,
					show: true,
					type: 'POST'
				}).then(res => {
					if (res.code == 1) {
						if (this.id) {
							uni.redirectTo({
								url: '/pagesA/check/make/make?id=' + this.id,
							})
						} else {
							uni.redirectTo({
								url: '/pagesA/check/listpeo/listpeo',
							})
						}
					}
				})
			}
		},
	}
</script>

<style lang="less">
	@import url("addpeo.less");
</style>
